<?php
/**
 * Created by PhpStorm.
 * User: WYJ
 * Date: 2018/5/8
 * Time: 15:39
 */

namespace zhtfrontend\modules\customermanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\Cate;
use common\models\datas\Customer;
use common\models\datas\Student;
use doumiaoduo\buildform\BuildForm;
use yii\data\Pagination;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use common\components\actions\DeleteAction;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\CustomerRelationship;
use zhtfrontend\models\datas\CustomerReturnVisit;

class ReturnvisitController extends BaseController
{

    //列表
    public function actionIndex()
    {

        $site_id = Fun::getSiteId();
        $param['searchData'] = \Yii::$app->request->get();
        $model = [];
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = CustomerReturnVisit::find()->where(['site_id' => $site_id]);

        $category = (new Query())->select('id,name')->from('{{%cate}}')->where(['parent_id' => 0])->all();

        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            //手机号搜索
            if (!empty($search_data['tel'])) {
                $data->andWhere(['mobile' => $search_data['tel']]);
            }
            //姓名搜索
            if (!empty($search_data['name'])) {
                $data->andWhere(['name' => $search_data['name']]);
            }
            //意向分类搜索
            if (!empty($search_data['category_id'])) {
                $data->andWhere(['category_id' => $search_data['category_id']]);
            }
            //获客渠道搜索
            if (!empty($search_data['source'])) {
                $data->andWhere(['type' => $search_data['type']]);
            }
            //客户类型搜索
            if (!empty($search_data['status'])) {
                $data->andWhere(['status' => $search_data['status']]);
            }
        }
        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'customer_manager') {
            $data->andWhere(['admin_id' => \Yii::$app->user->identity->id]);;
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        if (isset(\Yii::$app->user->identity) && in_array(\Yii::$app->user->identity->role, ['admin', 'administrator', 'sales_director', 'marketing_director'])) {
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        } elseif (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'sales_specialist') {
            $model = $data->andWhere(['admin_id' => \Yii::$app->user->id])->orWhere(['next_interviewer_id' => \Yii::$app->user->id])->all();
        } elseif (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'marketing_commissioner') {
            $salesman_id_array = [];
            $customer_relationship = (new Query())->select('*')->from('{{%customer_relationship}}')
                ->where(['not', ['admin_child_ids' => null]])->all(Fun::getSiteDb());
            foreach ($customer_relationship as $key => $val) {
                $val_array = explode(',', $val['admin_child_ids']);
                if (in_array(\Yii::$app->user->id, $val_array)) {
                    $salesman_id_array[] = $val['salesman_id'];
                }
            }
            $model = $data->andWhere(['in', 'admin_id', $salesman_id_array])->all();
        } else {
            if (!empty($param['searchData']['tel']) || !empty($param['searchData']['name'])) $model = $data->all();
        }
        $array = Fun::asArray($model, [
            'id', 'name', 'mobile', 'category_id', 'status', 'admin_id', 'operate', 'is_customer', 'is_student', 'is_buy', 'created_at', 'next_time', 'type', 'details', 'results'
        ]);
        return $this->render('index', [
            'data' => $array,
            'pages' => $pages,
            'category' => $category
        ]);

    }

    //添加
    public function actionCreate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new CustomerReturnVisit();
        $form = new BuildForm('@app/modules/customermanage/components/forms/returnvisitForm.php');
        $form->loadModel(['returnvisit' => $model]);
        $site_id = Fun::getSiteId();
        $user_id = \Yii::$app->user->id;

        $return_visit_query = (new Query())->select('*')->from('{{%customer_relationship}}')->where(['site_id' => Fun::getSiteId()]);

        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'sales_specialist') {
            $return_visit = $return_visit_query->andWhere(['or', 'admin' => \Yii::$app->user->id, 'next_interviewer_id' => \Yii::$app->user->id])->all(Fun::getSiteDb());
        } elseif (isset(\Yii::$app->user->identity) && in_array(\Yii::$app->user->identity->role, ['admin', 'administrator', 'sales_director'])) {
            $return_visit = $return_visit_query->andWhere(['NOT', ['admin_id' => null]])->all(Fun::getSiteDb());
        }
        $name_array = $mobile_array = [];
        foreach ($return_visit as $key => $val) {
            $name_array[$val['name']] = $val['name'];
            $mobile_array[$val['mobile']] = $val['mobile'];
        }

        //客户姓名
        $form->setData('returnvisit', 'name', 'data', $name_array);
        //客户联系方式
        $form->setData('returnvisit', 'mobile', 'data', $mobile_array);
        //$customer_relationship = CustomerRelationship::find()->where(['salesman_id' => $user_id, 'site_id' => Fun::getSiteId()])->all();

        //分类类别
        $category = Cate::find()->where(['parent_id' => 0])->asArray()->all();
        $form->setItems('returnvisit', 'category_id', ArrayHelper::map($category, 'id', 'name'));

        //当前约谈人
        $salesman_admin = Admin::find()->where(['status' => 1])->andWhere(['in', 'role', ['sales_director', 'sales_specialist']])->all();
        $form->setItems('returnvisit', 'interviewer_id', ArrayHelper::map($salesman_admin, 'id', 'name'));

        //下次约谈人
        $form->setItems('returnvisit', 'next_interviewer_id', ArrayHelper::map($salesman_admin, 'id', 'name'));

        //客户来源
        $type_arr = [
            ['id' => '0', 'name' => '电话'],
            ['id' => '1', 'name' => '约谈'],
            ['id' => '2', 'name' => '其他']
        ];
        $form->setItems('returnvisit', 'type', ArrayHelper::map($type_arr, 'id', 'name'));
        //客户类别
        $status_arr = [
            ['id' => '0', 'name' => '潜在客户'],
            ['id' => '1', 'name' => '意向客户'],
            ['id' => '2', 'name' => '意向可转化客户'],
            ['id' => '3', 'name' => '普通会员'],
            ['id' => '4', 'name' => 'VIP会员'],
        ];
        $form->setItems('returnvisit', 'status', ArrayHelper::map($status_arr, 'id', 'name'));

        //ajax 验证操作,
        if (\Yii::$app->request->post('ajax') == 'returnvisit-form' && $form->loadData(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $post = \Yii::$app->request->post()['CustomerReturnVisit'];
            $model = $form->getModel('returnvisit');
            $model->site_id = $site_id;
            $model->created_at = strtotime($post['created_at']);
            $model->next_time = strtotime($post['next_time']);
            $customer_relationship = (new Query())->select('*')->from('{{%customer_relationship}}')
                ->where(['mobile' => $post['mobile'], 'site_id' => Fun::getSiteId()])->one(Fun::getSiteDb());
            $model->relationship_id = $customer_relationship['id'];
            $model->interviewer_id = $post['interviewer_id'] == 0 ? $post['salesman_id'] : $post['interviewer_id'];
            $model->next_interviewer_id = $post['next_interviewer_id'] == 0 ? $post['salesman_id'] : $post['next_interviewer_id'];
            $model->admin_id = $customer_relationship['salesman_id'];
            $model->status = !empty($post['status']) ? $post['status'] : $customer_relationship['status'];
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['customermanage/returnvisit/index']));
            }
        }
        return $this->render('create', ['model' => $model, 'form' => $form->render()]);

    }

    //修改
    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        $site_id = Fun::getSiteId();
        if (!isset($id)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $model = CustomerReturnVisit::findOne($id);

        if (!isset($model)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $form = new BuildForm('@app/modules/customermanage/components/forms/returnvisitForm.php');
        $form->loadModel(['returnvisit' => $model]);

        //ajax 验证操作,
        if (\Yii::$app->request->post('ajax') == 'returnvisit-form' && $form->loadData(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('returnvisit');
            $model->site_id = $site_id;
            $model->created_at = strtotime(\Yii::$app->request->post()['CustomerReturnVisit']['created_at']);
            $model->next_time = strtotime(\Yii::$app->request->post()['CustomerReturnVisit']['next_time']);
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['customermanage/returnvisit/index']));
            }

        }

        return $this->render('create', ['form' => $form->render()]);

    }

    public function actionChange()
    {
        $this->layout = false;
        $id = \Yii::$app->request->get('id');
        $site_id = Fun::getSiteId();
        if (!isset($id)) {
            throw new NotFoundHttpException('信息不存在');
        }
        $model = CustomerReturnVisit::findOne($id);
        if (!isset($model)) {
            throw new NotFoundHttpException('信息不存在');
        }
        $form = new BuildForm('@app/modules/customermanage/components/forms/changeForm.php');
        $form->loadModel(['change' => $model]);
        //ajax 验证操作,
        if (\Yii::$app->request->post('ajax') == 'returnvisit-form' && $form->loadData(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->follow_status == 1) {
            return $this->redirectMessage('你不能对该客户信息进行此操作', Fun::url(['customermanage/returnvisit/index']));
        }
        if ($form->loadData(\Yii::$app->request->post())) {
            $post = \Yii::$app->request->post()['CustomerReturnVisit'];
            if ($post['change_status'] == 0) {
                $model->status = 2;
                if ($model->save(false)) {
                    $customer_relationship = CustomerRelationship::find()->where(['mobile' => $model->mobile, 'site_id' => Fun::getSiteId()])->one();
                    $customer_relationship->status = 2;
                    if ($customer_relationship->save(false)) {
                        $customer = Customer::find()->where(['mobile' => $model->mobile, 'site_id' => Fun::getSiteId()])->one();
                        if (!isset($customer)) {
                            if ((int)$model->status <= 1) {
                                return $this->redirectMessage('该客户目前的状态不能转化', Fun::url(['customermanage/returnvisit/index']));
                            } else {
                                $customer_model = new Customer();
                                $customer_model->name = $model->name;
                                $customer_model->site_id = Fun::getSiteId();
                                $customer_model->mobile = $model->mobile;
                                $customer_model->created_at = time();
                                if ($customer_model->save(false)) {
                                    return $this->redirectMessage('转化成功', Fun::url(['customermanage/returnvisit/index']));
                                }
                            }
                        } else {
                            return $this->redirectMessage('该客户已经是系统会员', Fun::url(['customermanage/returnvisit/index']));
                        }
                    }
                } else {
                    return $this->redirectMessage('操作失败', Fun::url(['customermanage/returnvisit/index']));
                }
            } else {
                return $this->redirectMessage('操作失败', Fun::url(['customermanage/returnvisit/index']));
            }
        }

        return $this->render('change', ['form' => $form->render()]);
    }

    //回收指派
    public function actionRecoveryassign()
    {
        $this->layout = false;
        $id = \Yii::$app->request->get('id');
        if (!isset($id)) {
            throw new NotFoundHttpException('信息不存在');
        }
        $model = CustomerReturnVisit::findOne($id);
        $admin_id = $model->admin_id;
        $mobile = $model->mobile;
        if (!isset($model)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $form = new BuildForm('@app/modules/customermanage/components/forms/recoveryForm.php');
        $form->loadModel(['recovery' => $model]);

        $salesman_admin = Admin::find()->where(['status' => 1])->andWhere(['in', 'role', ['sales_director', 'sales_specialist']])->all();
        $form->setItems('recovery', 'admin_id', ArrayHelper::map($salesman_admin, 'id', 'name'));

        //ajax 验证操作,
        if (\Yii::$app->request->post('ajax') == 'recovery-form' && $form->loadData(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post())) {
            $post = \Yii::$app->request->post()['CustomerReturnVisit'];
            if ($post['follow_status'] == 1) {
                CustomerReturnVisit::updateAll(['follow_status' => 1], ['mobile' => $mobile, 'admin_id' => $admin_id]);
                $customer_relationship = CustomerRelationship::find()->where(['mobile' => $model->mobile, 'site_id' => Fun::getSiteId()])->one();
                $customer_relationship->salesman_id = $post['admin_id'];
                if ($customer_relationship->save(false)) {
                    $return_visit_model = new CustomerReturnVisit();
                    $return_visit_model->name = $model->name;
                    $return_visit_model->status = $model->status;
                    $return_visit_model->mobile = $model->mobile;
                    $return_visit_model->created_at = $model->created_at;
                    $return_visit_model->details = '已回收';
                    $return_visit_model->results = '已回收';
                    $return_visit_model->admin_id = $admin_id;
                    $return_visit_model->category_id = $model->category_id;
                    $return_visit_model->site_id = Fun::getSiteId();
                    $return_visit_model->type = $model->type;
                    $return_visit_model->next_time = $model->created_at;
                    $return_visit_model->interviewer_id = $admin_id;
                    $return_visit_model->follow_status = 3;
                    $return_visit_model->relationship_id = $customer_relationship->id;
                    $return_visit_model->next_interviewer_id = $admin_id;
                    if ($return_visit_model->save(false)) {
                        return $this->redirectMessage('指派成功', Fun::url(['customermanage/returnvisit/index']));
                    }
                }
            } else {
                return $this->redirectMessage('请选择回收分派再保存信息', Fun::url(['customermanage/returnvisit/index']));
            }
        }

        return $this->render('recoveryassign', ['form' => $form->render()]);
    }

    //批量回收
    public function actionRecoveryassignall()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new CustomerReturnVisit();

        $data = (new Query())->select('*')->from('{{%customer_return_visit}}')
            ->where(['follow_status' => 0])->groupBy('mobile')->all(Fun::getSiteDb());

        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post())) {
            $post = \Yii::$app->request->post();
            if (!empty($post['ids']) && is_array($post['ids'])) {
                $admin_id = $post['CustomerReturnVisit']['admin_id'];
                $return_visit = (new Query())->select('*')->from('{{%customer_return_visit}}')
                    ->where(['in', 'relationship_id', $post['ids']])->groupBy('relationship_id')->all(Fun::getSiteDb());
                if (isset($return_visit)) {
                    foreach ($return_visit as $key => $val) {
                        $customer_relationship = CustomerRelationship::find()
                            ->where(['mobile' => $val['mobile'], 'site_id' => Fun::getSiteId()])
                            ->one();
                        $customer_relationship->salesman_id = $admin_id;
                        $customer_relationship->save(false);
                        $return_visit_model = new CustomerReturnVisit();
                        $return_visit_model->name = $val['name'];
                        $return_visit_model->status = $val['status'];
                        $return_visit_model->mobile = $val['mobile'];
                        $return_visit_model->created_at = $val['created_at'];
                        $return_visit_model->details = '已回收';
                        $return_visit_model->results = '已回收';
                        $return_visit_model->admin_id = $val['admin_id'];
                        $return_visit_model->category_id = $val['category_id'];
                        $return_visit_model->site_id = Fun::getSiteId();
                        $return_visit_model->type = $val['type'];
                        $return_visit_model->next_time = $val['created_at'];
                        $return_visit_model->interviewer_id = $val['admin_id'];
                        $return_visit_model->follow_status = 3;
                        $return_visit_model->next_interviewer_id = $val['admin_id'];
                        $return_visit_model->relationship_id = $val['id'];
                        $return_visit_model->save(false);
                    }
                    $return_visit_result = CustomerReturnVisit::updateAll(['follow_status' => 1], ['in', 'relationship_id', $post['ids']]);
                    if ($return_visit_result >= 1) {
                        return $this->redirectMessage(\Yii::t('zhtfrontend', '指派成功'), Fun::url(['customermanage/returnvisit/index']));
                    }
                } else {
                    return $this->redirectMessage(\Yii::t('zhtfrontend', '信息错误'), Fun::url(['customermanage/returnvisit/index']));

                }
            }
        }
        return $this->render('recoveryassignall', [
            'model' => $model,
            'data' => $data
        ]);
    }

    public function actionDetail()
    {
        $id = \Yii::$app->request->get('id');

        if (!isset($id)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $model = CustomerReturnVisit::find()->where(['id' => $id])->asArray()->one();

        if (!isset($model)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $customer = (new Query())->select('*')->from('{{%customer}}')
            ->where(['mobile' => $model['mobile'], 'site_id' => Fun::getSiteId()])->one();

        //意向分类
        $category = (new Query())->select('*')->from('{{%cate}}')->where(['id' => $model['category_id']])->one();
        $model['category_id'] = (isset($category)) ? $category['name'] : '暂无';

        //客户状态
        switch ($model['status']) {
            case 0:
                $model['status'] = '潜在客户';
                break;
            case 1:
                $model['status'] = '意向客户';
                break;
            case 2:
                $model['status'] = '正式VIP';
                break;
        }

        //约谈方式
        switch ($model['type']) {
            case 0:
                $model['type'] = '电话';
                break;
            case 1:
                $model['type'] = '地推';
                break;
            case 2:
                $model['type'] = '其他';
                break;
        }
        //跟进方式
        switch ($model['follow_status']) {
            case 0:
                $model['follow_status'] = '可跟进';
                break;
            case 1:
                $model['follow_status'] = '不可跟进';
                break;
        }

        //负责人
        $responsibility_man = (new Query())->select('*')->from('{{%admin}}')->where(['id' => $model['admin_id']])->one(Fun::getSiteDb());
        $model['admin_id'] = $responsibility_man['name'];

        //约谈人
        $interviewer = (new Query())->select('*')->from('{{%admin}}')->where(['id' => $model['interviewer_id']])->one(Fun::getSiteDb());
        $model['interviewer_id'] = $interviewer['name'];

        //是否注册
        $model['is_customer'] = (isset($customer)) ? '已注册' : '未注册';

        //是否绑定学员
        $model['is_student'] = '没有绑定学员';
        if (isset($customer)) {
            $student = Student::find()->where(['customer_id' => $customer['id']])->asArray()->all();
            if (isset($student)) {
                $model['is_student'] = '绑定' . count($student) . '位学员';
            }
        }

        //是否消费
        $model['is_buy'] = '该客户未消费过';
        if (isset($customer)) {
            $orders = (new Query())->select('*')->from('{{%orders}} o')
                ->leftJoin('{{%orders_items_relation}} oir', 'o.id=oir.orders_id')
                ->where(['oir.customer_id' => $customer['id']])->all(Fun::getSiteDb());
            if (isset($orders)) {
                $model['is_buy'] = '该客户购买过产品';
            }
        }
        $back_url = Fun::url(['customermanage/returnvisit/index']);
        return $this->render('detail', ['back_url' => $back_url, 'data' => $model]);
    }


    //销售人员客户列表添加记录
    public function actionAdd()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new CustomerReturnVisit();
        $form = new BuildForm('@app/modules/customermanage/components/forms/returnvisitaddForm.php');
        $form->loadModel(['returnvisitadd' => $model]);
        $site_id = Fun::getSiteId();
        $customer_relationship_id = \Yii::$app->request->get('id');
        if ($customer_relationship_id == null || $customer_relationship_id == 0) {
            return $this->redirectMessage('信息不存在', Fun::url(['customermanage/customerrelationship/index']));
        }
        $customer_relationship = (new Query())->select('*')->from('{{%customer_relationship}}')
            ->where(['id' => $customer_relationship_id])->one(Fun::getSiteDb());
        if ($customer_relationship == null) {
            return $this->redirectMessage('信息不存在', Fun::url(['customermanage/customerrelationship/index']));
        }

        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'sales_specialist') {
            $return_visit = (new Query())->select('*')->from('{{%customer_return_visit}}')
                ->where(['next_interviewer_id' => \Yii::$app->user->id, 'mobile' => $customer_relationship['mobile']])->all(Fun::getSiteDb());
            foreach ($return_visit as $key => $val) {
                if ($val['next_interviewer_id'] != 0 && $val['next_interviewer_id'] != $val['admin_id']) {
                    return $this->redirectMessage('您不能进行该项操作', Fun::url(['customermanage/customerrelationship/index']));
                }
            }
        }

        //当前约谈人
        $salesman_admin = Admin::find()->where(['status' => 1])->andWhere(['in', 'role', ['sales_director', 'sales_specialist']])->all();
        $form->setItems('returnvisitadd', 'interviewer_id', ArrayHelper::map($salesman_admin, 'id', 'name'));

        //下次约谈人
        $form->setItems('returnvisitadd', 'next_interviewer_id', ArrayHelper::map($salesman_admin, 'id', 'name'));

        //客户来源
        $type_arr = [
            ['id' => '0', 'name' => '电话'],
            ['id' => '1', 'name' => '约谈'],
            ['id' => '2', 'name' => '其他']
        ];
        $form->setItems('returnvisitadd', 'type', ArrayHelper::map($type_arr, 'id', 'name'));
        //客户类别
        $status_arr = [
            ['id' => '0', 'name' => '潜在客户'],
            ['id' => '1', 'name' => '意向客户'],
            ['id' => '2', 'name' => '意向可转化客户'],
            ['id' => '3', 'name' => '普通会员'],
            ['id' => '4', 'name' => 'VIP会员'],
        ];
        $form->setItems('returnvisitadd', 'status', ArrayHelper::map($status_arr, 'id', 'name'));

        //ajax 验证操作,
        if (\Yii::$app->request->post('ajax') == 'returnvisitadd-form' && $form->loadData(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $post = \Yii::$app->request->post()['CustomerReturnVisit'];
            $model->name = $customer_relationship['name'];
            $model->mobile = $customer_relationship['mobile'];
            $model->category_id = $customer_relationship['category_id'];
            $model->site_id = $site_id;
            $model->created_at = strtotime($post['created_at']);
            $model->next_time = strtotime($post['next_time']);
            $model->interviewer_id = $post['interviewer_id'] == 0 ? $post['salesman_id'] : $post['interviewer_id'];
            $model->next_interviewer_id = $post['next_interviewer_id'] == 0 ? $post['salesman_id'] : $post['next_interviewer_id'];
            $model->admin_id = $customer_relationship['salesman_id'];
            $model->status = !empty($post['status']) ? $post['status'] : $customer_relationship['status'];
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['customermanage/customerrelationship/index']));
            }
        }
        return $this->render('create', ['model' => $model, 'form' => $form->render()]);
    }

    //销售人员客户列表查看记录
    public function actionShow()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        if (!isset($id)) {
            throw new NotFoundHttpException('信息不存在');
        }
        $user_id = \Yii::$app->user->id;//用户id
        if (!isset($user_id)) {
            throw new NotFoundHttpException('信息不存在');
        }

        $return_visit_query = (new Query())->select('*')->from('{{%customer_return_visit}}');
        if (isset(\Yii::$app->user->identity) && in_array(\Yii::$app->user->identity->role, ['admin', 'administrator', 'sales_director'])) {
            $return_visit = $return_visit_query->all(Fun::getSiteDb());
        } elseif (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'sales_specialist') {
            $return_visit = $return_visit_query->andWhere(['admin_id' => \Yii::$app->user->id])->orWhere(['next_interviewer_id' => \Yii::$app->user->id])
                ->all(Fun::getSiteDb());
        }
        $array = [];
        if (!empty($return_visit) && !empty($return_visit[0])) {
            switch ($return_visit[0]['follow_status']) {
                case 0:
                    $mobile = $return_visit[0]['mobile'];
                    $array = (new Query())->select('*')->from('{{%customer_return_visit}}')->where(['mobile' => $mobile])->all(Fun::getSiteDb());
                    break;
                case 1:
                    $array = $return_visit;
                    break;
            }
        }
        return $this->render('show', ['data' => $array]);
    }


    //删除
    public function actions()
    {
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => CustomerReturnVisit::className(),
                'message' => ['success' => '删除成功！'],
                'redirectTo' => 'returnvisit/index',
            ],
        ];
    }

}